package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderAccept;
import com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptItem;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseOrderAcceptImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptItemImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderImpl;
import com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderAcceptProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCPURCHASEORDERACCEPT_TYPE")
public class RCPurchaseOrderAcceptImpl extends RCBasePurchaseOrderAcceptImpl implements RCPurchaseOrderAccept {
  public RCPurchaseOrderAcceptImpl() {
    super();
  }
  
  public RCPurchaseOrderAcceptImpl(final RCPurchaseOrder purchaseOrder) {
    super();
    this.purchaseOrder = purchaseOrder;
  }
  
  @JoinColumn(name = "purchaseOrder_id")
  @ManyToOne(targetEntity = RCPurchaseOrderImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCPurchaseOrder purchaseOrder;
  
  public RCPurchaseOrder getPurchaseOrder() {
    return this.purchaseOrder;
  }
  
  public RCPurchaseOrderAccept setPurchaseOrder(final RCPurchaseOrder purchaseOrder) {
    this.purchaseOrder = purchaseOrder;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCPurchaseOrderAcceptItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "accept")
  private Collection<RCPurchaseOrderAcceptItem> acceptItems = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptItem>();;
  
  public Collection<RCPurchaseOrderAcceptItem> getAcceptItems() {
    return this.acceptItems;
  }
  
  public RCPurchaseOrderAcceptItem createAndAddtoAcceptItems() {
    getAcceptItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptItem rCPurchaseOrderAcceptItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptItemImpl(this);
    getObjectFactory().create(rCPurchaseOrderAcceptItem);
    getAcceptItems().add(rCPurchaseOrderAcceptItem);
    return rCPurchaseOrderAcceptItem;
    
  }
  
  public RCPurchaseOrderAcceptItem createAndAddtoAcceptItems(final Procedure1<RCPurchaseOrderAcceptItem> updater) {
    getAcceptItems().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptItem rCPurchaseOrderAcceptItem = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptItemImpl(this);
    getObjectFactory().create(rCPurchaseOrderAcceptItem);
    getAcceptItems().add(rCPurchaseOrderAcceptItem);
    return rCPurchaseOrderAcceptItem;
    
  }
  
  public RCPurchaseOrderAccept removeAllAcceptItems() {
    for(RCPurchaseOrderAcceptItem obj : this.acceptItems){
    	getObjectFactory().delete(obj);
    }
    this.acceptItems.clear();
    return this;	
    
  }
  
  public RCPurchaseOrderAccept removeFromAcceptItems(final RCPurchaseOrderAcceptItem rCPurchaseOrderAcceptItem) {
    this.acceptItems.remove(rCPurchaseOrderAcceptItem);
    getObjectFactory().delete(rCPurchaseOrderAcceptItem);
    return this;
    
  }
  
  /**
   * 到货总金额
   */
  public RCProjectMoney getTotalAmount() {
    final Function1<RCPurchaseOrderAcceptItem,RCProjectMoney> _function = new Function1<RCPurchaseOrderAcceptItem,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseOrderAcceptItem it) {
          RCPurchaseOrderItem _orderItem = it.getOrderItem();
          RCProjectMoney _unitPrice = _orderItem.getUnitPrice();
          Integer _quantity = it.getQuantity();
          int _intValue = _quantity.intValue();
          RCProjectMoney _multi = _unitPrice.multi(Double.valueOf(((double) _intValue)));
          return _multi;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseOrderAcceptItem, RCProjectMoney>map(this.acceptItems, _function);
    RCExchangeRate _defaultExchangeRate = this.purchaseOrder.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  public static RCPurchaseOrderAccept create(final RCPurchaseOrder purchaseOrder, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCPurchaseOrderAccept rCPurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptImpl(
    	purchaseOrder
    );
    objectFactory.create(rCPurchaseOrderAccept);
    return rCPurchaseOrderAccept;			
    
  }
  
  public static RCPurchaseOrderAccept createRCPurchaseOrderAcceptImpl(final RCPurchaseOrder purchaseOrder, final IObjectFactory objectFactory, final Procedure1<RCPurchaseOrderAccept> init) {
    com.shrct.businessprocess.purchase.RCPurchaseOrderAccept rCPurchaseOrderAccept = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptImpl(
    	purchaseOrder
    );
    objectFactory.create(rCPurchaseOrderAccept);
    init.apply(rCPurchaseOrderAccept);
    
    return rCPurchaseOrderAccept;			
    
  }
  
  public RCPurchaseOrderAcceptProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderAcceptProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCPurchaseOrderAcceptProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
